<?php

require_once("vssession.php");

class VsImageAntibot {

	private $_textCode = '';
	private $_imageCode = NULL;	// Gd Resource

	private $_imageFormat = 'png';
	private $_imageWidth = 372;
    private $_imageHeight = 60;
    private $_imageBgColor = 'fff';

	// Generator Params
	public  $_noiseSquare = 0;
    public  $_noiseGranular = 0;
	private $_textLength = 7;

    public  $_textFont = "sans.ttf";
    public  $_textFontSize;
    public  $_textColorMode = 1;
    public  $_textSizeMode = 0.8;
    public  $_textRotationMode = 10;

    private $_textChars = "23456789abcdefghkmnpqrstuvwxyz"; 
    private $_colorList = array();

	function __construct($imgWidth = 372, $imgHeight = 60, $colorBg = "fff") {
		$this->_imageWidth = (int)$imgWidth;
		$this->_imageHeight = (int)$imgHeight;
		$this->_textFontSize = ((int)$this->_imageHeight) * 0.6;

		$this->_imageCode = imagecreatetruecolor($this->_imageWidth, $this->_imageHeight);
		
		list($r, $g, $b) = $this->html2rgb($colorBg);
		$this->_imageBgColor = imagecolorallocate($this->_imageCode, $r, $g, $b);
		imagefill($this->_imageCode, 0, 0, $this->_imageBgColor);
	}
	//============================================================================================

	public function __destruct() {
		imagedestroy($this->_imageCode);
	}
	//===========================================================================================

	function setTextLength($len) {
		if($len > 25)
			$len = 25;

        $this->_textLength = (int)$len;
	}
	//==========================================================================================

	function show() {
		$session  = VsSession::Instance();
		$session->assign('SecureImageText', $this->_textCode);

		$this->drawImage();
	}
	//=========================================================================================

	private function drawImage() {
		$this->drawNoise();
		$this->drawCode();

		imageantialias($this->_imageCode, true);
		imageinterlace($this->_imageCode, true);

		if($this->format == "png") {      
			header("Content-type: image/png");
			imagepng($this->_imageCode);
		}
		else {
			header("Content-type: image/jpeg");
			imagejpeg($this->_imageCode, "", 75);
		}
	}
	//===========================================================================================================

	private function drawNoise() {
		if($this->_noiseSquare > 0) {
			imagesetthickness($this->_imageCode, 1);
			
			for($i = 0; $i <= $this->_imageWidth; $i += ($this->_imageHeight/5)) {
				imageline($this->_imageCode, $i, 0, $i, $this->_imageHeight, $this->_colorList["black"]);
			}

			for($i = 0; $i <= $this->_imageHeight; $i += ($this->_imageHeight/5)) {
				imageline($this->_imageCode, 0, $i, $this->_imageWidth, $i, $this->_colorList["black"]);
			}
		}

		if($this->_noiseGranular > 0) {
			for($i = 1; $i < ($this->img_width * $this->img_width/10); $i++) {
				$cor_x = mt_rand(1, $this->img_width);
				$cor_y = mt_rand(1, $this->img_height);
				imagesetpixel($this->_imageCode, $cor_x, $cor_y, $this->color_list["black"]);
            }
        }

    }
	//============================================================================================================

	private function drawCode() {
		$securityCode = $this->generateCode();

		$x = $this->_imageWidth/18;

		for($i = 0; $i < $this->_textLength; $i++) {

			if($this->text_color_mode > 0)
				$color = imagecolorallocate($this->_imageCode, mt_rand(0, 250), mt_rand(0, 250), mt_rand(0, 250));
			else
				$color = $this->_colorList["black"];

			$textbox = imagettfbbox($this->_textFontSize, 0, $this->_textFont, $securityCode[$i]) or exit("There is an error in imagettfbbox function!");

			$w = abs($textbox[4] - $textbox[0]);

			$Size = mt_rand($this->_textFontSize * $this->_textSizeMode, $this->_textFontSize);
			$Angle = mt_rand(-$this->textRotationMode, $this->_textRotationMode);
			$y = $this->_imageHeight - $this->_imageHeight/4;
			$x = $x + ($w*1.1);

			imagettftext($this->_imageCode, $Size, $Angle, $x-$w, $y, $color, $this->text_font, $securityCode[$i]);
		}

	}
	//======================================================================================================================================

	private function generateCode() {
		$textCode = "";
		$textCodeChars = array();

		for($i = 0; $i < $this->_textLength; $i++) {
			$textCodeChars[$i] = substr($this->_textChars, mt_rand(0, strlen($this->_textChars) - 1), 1);
			$textCode .=  $textCodeChars[$i];
		}
		$this->_textCode = $textCode;

		return  $textCodeChars;
	}
	//====================================================================================================================================

	private function createListColor() {
		$this->_colorList["white"] = imagecolorallocate($im, 255, 255, 255);
		$this->_colorList["grey"] = imagecolorallocate($im, 238, 238, 238);
		$this->_colorList["black"] = imagecolorallocate($im, 0, 0, 0);
		$this->_colorList["red"] = imagecolorallocate($im, 255, 0, 0);

		list($r, $g, $b) = $this->html2rgb("0000ff");
		$this->_colorList["blue"] = imagecolorallocate($this->image_resource, $r, $g, $b);
	}
	//====================================================================================================================================

	private function html2rgb($color) {
		if($color[0] == "#") {
			$color = substr($color, 1);
		}

		if(strlen($color) == 6) {
			list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]);
		}
		else if(strlen($color) == 3) {
			list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
		}
		else {
			return false;
		}

		$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);    
		return array($r, $g, $b);
    }
	//=====================================================================================================================================

};

?>
